"""
urllib实现爬虫
"""
from urllib.request import urlopen, Request
import ssl

bing_url = 'http://www.bing.com'
"""
urllib.request.OpenerDirector中定义了默认的爬虫User-agent
    client_version = "Python-urllib/%s" % __version__
    self.addheaders = [('User-agent', client_version)]
"""
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'

request = Request(bing_url)
request.add_header("User-agent", user_agent)
"""
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)>
通过自定义ssl._create_unverified_context()解决以上问题: 忽略不信任的证书
"""
context = ssl._create_unverified_context()
response = urlopen(request, context=context)

# response = urlopen(bing_url)
print(response.closed)

# with关键字自动执行了response.close()方法
with response:
    print(type(response))
    # http状态码
    print(response.status)
    # http请求方式
    print(response._method)
    # 返回访问内容
    print(response.read())
    # 访问的真实url: http://www.bing.com -> http://cn.bing.com/
    print(response.geturl())
    # http响应头 Response Headers
    print(response.info())

print(response.closed)
